//
//  RHMeDeviceEndpointCell.m
//  RuiHeApp
//
//  Created by Yan Chen on 2025/1/7.
//

#import "RHMeDeviceEndpointCell.h"
#import "RHMeEndpointTypeModel.h"
#import "RHDeviceDetailModel.h"

@interface RHMeDevEndpointItemCell()
@property (nonatomic, strong) QMUILabel *statusLab;

@property (nonatomic, strong) QMUILabel *endpointNameLab;

@property (nonatomic, strong) UIImageView *icon;

@property (nonatomic, strong) UIView *shadowView;
@end
@implementation RHMeDevEndpointItemCell

-(instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame]){
        [self setupUI];
    }
    return self;
}

-(void)setEndpointTypeModel:(RHMeEndpointTypeModel *)endpointTypeModel
{
    _endpointTypeModel = endpointTypeModel;
    self.endpointNameLab.text = endpointTypeModel.endpointTypeCode;
    if ([endpointTypeModel.endpointTypeCode.uppercaseString isEqualToString:@"PACK"]) {
        self.endpointNameLab.text = [NSString stringWithFormat:@"%@ >",endpointTypeModel.endpointTypeCode];
    }
    self.statusLab.hidden = YES;
    NSString *imageName;
    NSInteger state = [endpointTypeModel.state integerValue];
   
    
    
    //在线状态
    if(state == 0){
        //离线
        self.statusLab.hidden = NO;
        self.statusLab.textColor = kCOLOR_HEX(@"#8B8D92");
        self.statusLab.text = @"离线".rh_localized;
        imageName = [self getEndpointImageWithEndpointName:endpointTypeModel.endpointTypeCode status:state];
    }else if(state == 1){
        //在线
        self.statusLab.textColor = kCOLOR_HEX(@"#05C79D");
        self.statusLab.hidden = YES;
        imageName = [self getEndpointImageWithEndpointName:endpointTypeModel.endpointTypeCode status:state];
    }else if(state == 2){
        //故障
        self.statusLab.textColor = kCOLOR_HEX(@"#F95454");
        self.statusLab.hidden = NO;
        self.statusLab.text = @"故障".rh_localized;
        imageName = [self getEndpointImageWithEndpointName:endpointTypeModel.endpointTypeCode status:state];
    }else if(state == 3){
        //告警
        self.statusLab.textColor = kCOLOR_HEX(@"#F0A330");
        self.statusLab.hidden = NO;
        self.statusLab.text = @"告警".rh_localized;
        imageName = [self getEndpointImageWithEndpointName:endpointTypeModel.endpointTypeCode status:state];
    }
    self.icon.image = [UIImage imageNamed:imageName];
//
//    //故障状态
//    
//    if(model.normal == 2){
//        //故障
//        self.statusLab.hidden = NO;
//        self.statusLab.textColor = kCOLOR_HEX(@"#F0A330");
//        self.statusLab.text = @"故障".rh_localized;
//    }else if(model.normal == 1){
//        self.statusLab.hidden = NO;
//        self.statusLab.textColor = kCOLOR_HEX(@"#F95454");
//        self.statusLab.text = @"告警".rh_localized;
//    }
    
    [self.statusLab sizeToFit];
    [self.statusLab mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.offset(self.statusLab.width+20);
    }];
    self.shadowView.hidden = self.statusLab.isHidden;
    
}

-(NSString*)getEndpointImageWithEndpointName:(NSString*)endpointCode  status:(NSInteger)status
{
    NSString *endpointTypeCode = endpointCode;
    if ([endpointCode.uppercaseString hasPrefix:@"PCS"]) {
        endpointTypeCode = @"PCS";
    }
    if ([endpointCode.uppercaseString hasPrefix:@"INV"]) {
        endpointTypeCode = @"INV";
    }
    if ([endpointCode.uppercaseString hasPrefix:@"DEHUM"]) {
        endpointTypeCode = @"DEHUM";
    }
    
    NSDictionary *dic = @{
        @"EMS":   @"dev_endpoint_ems_offline",
        @"EMU":   @"dev_endpoint_ems_offline",
        @"MASTER":@"dev_endpoint_master_offline",
        @"BMS":   @"dev_endpoint_master_offline",
        @"PACK":  @"dev_endpoint_battery_offline",
        @"CLUSTER":  @"dev_endpoint_battery_offline",
        @"PCS":   @"dev_endpoint_pcs_offline",
        @"INV":   @"dev_endpoint_pcs_offline",
        @"LCU":   @"dev_endpoint_lcu_offline", //液冷
        @"METER": @"dev_endpoint_meter_offline",//电表
        @"DEHUM": @"dev_endpoint_dehum_offline",//除湿
        @"ACU":   @"dev_endpoint_acu_offline",//风冷
        @"HVB":   @"dev_endpoint_hvb_offline",//绝缘
        @"BATTERY":   @"dev_endpoint_battery2_offline",//电池
        @"DCDC":   @"dev_endpoint_dcdc_offline"//DCDC
    };
    if (status == 1) {//在线图标
        dic = @{
            @"EMS":   @"dev_endpoint_ems_online",
            @"EMU":   @"dev_endpoint_ems_online",
            @"MASTER":@"dev_endpoint_master_online",
            @"BMS":   @"dev_endpoint_master_online",
            @"PACK":  @"dev_endpoint_battery_online",
            @"CLUSTER":  @"dev_endpoint_battery_online",
            @"PCS":   @"dev_endpoint_pcs_online",
            @"INV":   @"dev_endpoint_pcs_online",
            @"LCU":   @"dev_endpoint_lcu_online", //液冷
            @"METER": @"dev_endpoint_meter_online",//电表
            @"DEHUM": @"dev_endpoint_dehum_online",//除湿
            @"ACU":   @"dev_endpoint_acu_online",//风冷
            @"HVB":   @"dev_endpoint_hvb_online",//绝缘
            @"BATTERY":   @"dev_endpoint_battery2_online",//电池
            @"DCDC":   @"dev_endpoint_dcdc_online"//dcdc
        };
    }else if(status == 3) {//告警图标
        dic = @{
            @"EMS":   @"dev_endpoint_ems_fault",
            @"EMU":   @"dev_endpoint_ems_fault",
            @"MASTER":@"dev_endpoint_master_fault",
            @"BMS":   @"dev_endpoint_master_fault",
            @"PACK":  @"dev_endpoint_battery_fault",
            @"CLUSTER":  @"dev_endpoint_battery_fault",
            @"PCS":   @"dev_endpoint_pcs_fault",
            @"INV":   @"dev_endpoint_pcs_fault",
            @"LCU":   @"dev_endpoint_lcu_fault", //液冷
            @"METER": @"dev_endpoint_meter_fault",//电表
            @"DEHUM": @"dev_endpoint_dehum_fault",//除湿
            @"ACU":   @"dev_endpoint_acu_fault",//风冷
            @"HVB":   @"dev_endpoint_hvb_fault",//绝缘
            @"BATTERY":   @"dev_endpoint_battery2_fault",//电池
            @"DCDC":   @"dev_endpoint_dcdc_fault"//DCDC
        };
    }else if(status == 2) {//故障图标
        dic = @{
            @"EMS":   @"dev_endpoint_ems_alarm",
            @"EMU":   @"dev_endpoint_ems_alarm",
            @"MASTER":@"dev_endpoint_master_alarm",
            @"BMS":   @"dev_endpoint_master_alarm",
            @"PACK":  @"dev_endpoint_battery_alarm",
            @"CLUSTER":  @"dev_endpoint_battery_alarm",
            @"PCS":   @"dev_endpoint_pcs_alarm",
            @"INV":   @"dev_endpoint_pcs_alarm",
            @"LCU":   @"dev_endpoint_lcu_alarm", //液冷
            @"METER": @"dev_endpoint_meter_alarm",//电表
            @"DEHUM": @"dev_endpoint_dehum_alarm",//除湿
            @"ACU":   @"dev_endpoint_acu_alarm",//风冷
            @"HVB":   @"dev_endpoint_hvb_alarm",//绝缘
            @"BATTERY":   @"dev_endpoint_battery2_alarm",//电池
            @"DCDC":   @"dev_endpoint_dcdc_alarm"//DCDC
        };
    }
    
    return dic[endpointTypeCode.uppercaseString];
}

-(void)setupUI
{
    self.backgroundColor = UIColor.clearColor;
    self.contentView.backgroundColor = UIColor.clearColor;
    kWeakSelf;
    UIView *bgView = kView;
    bgView.backgroundColor = kCOLOR_HEX(@"#FCFBFB");
    bgView.layer.borderColor = kBorderColor.CGColor;
    bgView.layer.borderWidth = 1;
    [self.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 0, 0, 0));
        make.height.offset(kUI_WINDOW_W*0.5);
    }];
   
    
    
    _icon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"plant_topo_device_0"]];
    [bgView addSubview:_icon];
    [_icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(60, 60)));
        make.centerX.mas_offset(0);
        make.top.mas_offset(15);
    }];
    
    _endpointNameLab = [QMUILabel labelWithFont:kSYSTEMFONT(10) textColor:kCOLOR_HEX(@"#8B8D92") textAligment:NSTextAlignmentCenter];
    _endpointNameLab.text = @"--";
    [bgView addSubview:_endpointNameLab];
    [_endpointNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.icon.mas_bottom).offset(15);
        make.left.right.mas_offset(0);
    }];
    
    
    _statusLab = [QMUILabel labelWithFont:kSYSTEMFONT(10) textColor:kCOLOR_HEX(@"#8B8D92") textAligment:NSTextAlignmentCenter];
    _statusLab.backgroundColor = UIColor.whiteColor;
    _statusLab.layer.cornerRadius = 10;
    _statusLab.layer.masksToBounds = YES;
    [bgView addSubview:_statusLab];
    [_statusLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(15);
        make.right.mas_offset(-15);
        make.height.offset(20);
    }];
    
    UIView *shadowView = kView;
    shadowView.backgroundColor = UIColor.whiteColor;
    shadowView.layer.shadowColor = kCOLOR_HEX_A(@"#000000", 0.5).CGColor;
    shadowView.layer.shadowRadius = 5;
    shadowView.layer.shadowOpacity = 0.5;
    shadowView.layer.cornerRadius = 10;
    shadowView.layer.shadowOffset = CGSizeMake(0, 0.5);
    [bgView addSubview:shadowView];
    [shadowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(weakSelf.statusLab);
    }];
    self.shadowView = shadowView;
    [bgView bringSubviewToFront:weakSelf.statusLab];
}

@end

//========================================================================================================

#define itemWidth  (kUI_WINDOW_W - 30)/3
@interface RHMeDeviceEndpointCell()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UIImageView *deviceIcon;

@property (nonatomic, strong) QMUILabel *statusLab;

@property (nonatomic, strong) QMUILabel *socLab;

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) UICollectionViewFlowLayout *layout;

@end
@implementation RHMeDeviceEndpointCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupUI];
    }
    return self;
}

-(void)setInfoModel:(RHDeviceDetailModel *)infoModel
{
    _infoModel = infoModel;
    NSString *soc = [NSDecimalNumber decimalStringWithDoubleString:infoModel.soc nullStr:@"--"];
    self.socLab.text = [NSString stringWithFormat:@"SOC:%@%%",soc];
    if (infoModel.status.integerValue == 0) {//在线
        switch ([infoModel.workStatus intValue]) {
            case 0:
                self.statusLab.text = @"待机".rh_localized;
                break;
            case 1:
                self.statusLab.text = @"充电".rh_localized;
                break;
            case 2:
                self.statusLab.text = @"放电".rh_localized;
                break;
            default:
                self.statusLab.text = @"--";
                break;
        }
    }else{
        //离线
        self.statusLab.text = @"离线".rh_localized;
    }
    
    [self setDeviceSoc];
    
    CGFloat height = 0;
    NSInteger count = infoModel.endpointDataArray.count;
    NSInteger row = count%3?count/3+1:count/3;
    height = row*itemWidth;
   
    [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.offset(height);
    }];

    [self.collectionView reloadData];
}

-(void)setDeviceSoc
{
    NSInteger soc = [self.infoModel.soc integerValue];
    NSString *imageStr = @"plant_topo_device_0";
    if (soc>0&&soc<15) {
        imageStr = @"plant_topo_device_10";
    }else if (soc>=15&&soc<25){
        imageStr = @"plant_topo_device_20";
    }else if (soc>=25&&soc<35){
        imageStr = @"plant_topo_device_30";
    }else if (soc>=35&&soc<45){
        imageStr = @"plant_topo_device_40";
    }else if (soc>=45&&soc<55){
        imageStr = @"plant_topo_device_50";
    }else if (soc>=55&&soc<65){
        imageStr = @"plant_topo_device_60";
    }else if (soc>=65&&soc<75){
        imageStr = @"plant_topo_device_70";
    }else if (soc>=75&&soc<85){
        imageStr = @"plant_topo_device_80";
    }else if (soc>=85&&soc<100){
        imageStr = @"plant_topo_device_90";
    }else if (soc>=100){
        imageStr = @"plant_topo_device_100";
    }
    self.deviceIcon.image = [UIImage imageNamed:imageStr];
}


- (void)setupUI
{
    self.contentView.backgroundColor = CFTabViewBgColor;
    kWeakSelf;
    UIView *bgView = kView;
    bgView.backgroundColor = UIColor.whiteColor;
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    bgView.qmui_borderColor = kBorderColor;
    bgView.qmui_borderWidth = 1;
    bgView.qmui_borderPosition = QMUIViewBorderPositionLeft|QMUIViewBorderPositionRight|QMUIViewBorderPositionTop|QMUIViewBorderPositionBottom;
    [self.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(0);
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.bottom.mas_offset(0);
    }];
    
    QMUILabel *titleLab = [QMUILabel labelWithFont:kBOLDSYSTEMFONT(16) textColor:kCOLOR_HEX(@"#303133") textAligment:NSTextAlignmentLeft];
    titleLab.text = @"设备拓扑图".rh_localized;
    [bgView addSubview:titleLab];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_offset(15);
        make.right.mas_offset(-15);
    }];
    
    //状态标签
    UIView *statusView = kView;
    [bgView addSubview:statusView];
    [statusView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(titleLab);
        make.right.mas_offset(-10);
    }];
    __block QMUILabel *lastLab;
    __block CGFloat statusLabelWidth = 0;
    NSArray *statusTitles = @[@"正常".rh_localized,@"离线".rh_localized,@"告警".rh_localized,@"故障".rh_localized];
    NSArray *colorArr = @[@"#05C79D",@"#8B8D92",@"#F0A330",@"#F95454"];
    for (int i = 0; i<statusTitles.count; i++) {
        QMUILabel *statusLab = [QMUILabel labelWithFont:kSYSTEMFONT(10) textColor:kCOLOR_HEX(@"#8B8D92") textAligment:NSTextAlignmentCenter];
        statusLab.text = statusTitles[i];
        statusLab.textColor = kCOLOR_HEX(colorArr[i]);
        statusLab.layer.cornerRadius = 10;
        statusLab.layer.masksToBounds = YES;
        [statusLab sizeToFit];
        [statusView addSubview:statusLab];
        [statusLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_offset(10);
            make.bottom.mas_offset(-10);
            if (lastLab) {
                make.left.equalTo(lastLab.mas_right).offset(10);
                statusLabelWidth += 10;
            }else{
                make.left.mas_offset(0);
            }
            make.width.offset(statusLab.width+20);
            make.height.offset(22);
            statusLabelWidth += statusLab.width+20;
        }];
        
        UIView *shadowView = kView;
        shadowView.backgroundColor = UIColor.whiteColor;
        shadowView.layer.shadowColor = kCOLOR_HEX_A(@"#000000", 0.5).CGColor;
        shadowView.layer.shadowRadius = 5;
        shadowView.layer.shadowOpacity = 0.5;
        shadowView.layer.cornerRadius = 10;
        shadowView.layer.shadowOffset = CGSizeMake(0, 0.5);
        [statusView addSubview:shadowView];
        [shadowView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.equalTo(statusLab);
        }];
        [statusView bringSubviewToFront:statusLab];
        lastLab = statusLab;
        
    }
    [lastLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(0);
    }];
    
    [titleLab sizeToFit];
    if (titleLab.width > kUI_WINDOW_W - 60-20-statusLabelWidth) {
        [statusView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(titleLab.mas_bottom).offset(10);
            make.right.mas_offset(-15);
        }];
    }
    
    _deviceIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"plant_topo_device_0"]];
    [bgView addSubview:_deviceIcon];
    [_deviceIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(32, 46)));
        make.centerX.mas_offset(0);
        make.top.equalTo(statusView.mas_bottom).offset(25);
    }];
    
    _statusLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#8B8D92") textAligment:NSTextAlignmentCenter];
    _statusLab.text = @"--";
    
    
    [bgView addSubview:_statusLab];
    [_statusLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.deviceIcon.mas_bottom).offset(15);
        make.left.right.mas_offset(0);
    }];
    
    _socLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#303133") textAligment:NSTextAlignmentCenter];
    _socLab.text = @"SOC:--";
    [bgView addSubview:_socLab];
    [_socLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.statusLab.mas_bottom).offset(5);
        make.left.right.mas_offset(0);
    }];
    
    _layout = [[UICollectionViewFlowLayout alloc]init];
    _layout.scrollDirection =  UICollectionViewScrollDirectionVertical;
    
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:_layout];
    [_collectionView registerClass:[RHMeDevEndpointItemCell class] forCellWithReuseIdentifier:@"RHMeDevEndpointItemCell"];
    _collectionView.backgroundColor = [UIColor clearColor];
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.pagingEnabled = YES;
    [bgView addSubview:_collectionView];
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.height.offset(0);
        make.bottom.mas_offset(0);
        make.top.equalTo(weakSelf.socLab.mas_bottom).offset(10);
        make.centerX.mas_offset(0);
    }];
}



-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
   
    return self.infoModel.endpointDataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    RHMeDevEndpointItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RHMeDevEndpointItemCell" forIndexPath:indexPath];
    cell.endpointTypeModel = [self.infoModel.endpointDataArray safeObjectAtIndex:indexPath.item];
    
    return cell;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    return CGSizeMake((int)itemWidth, (int)itemWidth);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return  UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.clickDeviceBlock) {
        self.clickDeviceBlock(self.infoModel.endpointDataArray[indexPath.row]);
    }
}



@end


